package fr.imag.ufrima.ctte.gui.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.Task;

public class FindDialog extends JDialog {
	private ImagCTTE imagCTTE;
	/**
	 * Constructor
	 */
	public FindDialog(ImagCTTE imagCTTE){
		this.imagCTTE = imagCTTE;
		InitComponents();		
		this.setLocationRelativeTo(null);
		btnFind.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent arg0) {
				if (getImagCTTE().getCurrentTaskDiagram() != null){
					String identifier = txtFind.getText();
					Task task = getImagCTTE().getCurrentTaskDiagram().getRootTask().findTask(identifier);
					if (task != null){
						getImagCTTE().getCurrentTaskDiagram().setSelectedTask(task);
						FindDialog.this.dispose();
					}else{
						JOptionPane.showMessageDialog(null, "No task with this identifier!");
					}
				}
			}
		});
		btnCancel.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent arg0) {
				FindDialog.this.dispose();
			}
		});
	}
	/**
	 * Initialize componenets
	 */
	protected void InitComponents(){

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblFind = new javax.swing.JLabel();
        btnFind = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        txtFind = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        ckbMatchCase = new javax.swing.JCheckBox();
        ckbAllWord = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        rdbSearchAll = new javax.swing.JRadioButton();
        rdbSearchSubTree = new javax.swing.JRadioButton();
        lblResults = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstResults = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find");

        lblFind.setText("Find:");

        btnFind.setText("Find");

        btnCancel.setText("Cancel");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        ckbMatchCase.setText("Match case");

        ckbAllWord.setText("All the word");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ckbMatchCase)
                    .addComponent(ckbAllWord)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(ckbMatchCase)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ckbAllWord))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        buttonGroup1.add(rdbSearchAll);
        rdbSearchAll.setSelected(true);
        rdbSearchAll.setText("Search in all trees");

        buttonGroup1.add(rdbSearchSubTree);
        rdbSearchSubTree.setText("Search in selected subtree");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdbSearchAll)
                    .addComponent(rdbSearchSubTree))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(rdbSearchAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbSearchSubTree))
        );

        lblResults.setText("Researche results:");

        jScrollPane1.setViewportView(lstResults);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblFind)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFind, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnFind))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancel))
                    .addComponent(lblResults, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnFind});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnFind)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblFind)
                        .addComponent(txtFind, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCancel)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblResults)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
	}
	/**
	 * Components
	 */
	private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnFind;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox ckbAllWord;
    private javax.swing.JCheckBox ckbMatchCase;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFind;
    private javax.swing.JLabel lblResults;
    private javax.swing.JList lstResults;
    private javax.swing.JRadioButton rdbSearchAll;
    private javax.swing.JRadioButton rdbSearchSubTree;
    private javax.swing.JTextField txtFind;
	public ImagCTTE getImagCTTE() {
		return imagCTTE;
	}
	public void setImagCTTE(ImagCTTE imagCTTE) {
		this.imagCTTE = imagCTTE;
	}
}
